<?php
if (!defined('BASEPATH'))
	exit('No direct script access allowed');

class MY_Controller extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->requireLogin();
	}

	protected function renderView($viewName, $data = null)
	{
		$this->load->view("common/header", $data);
		$this->load->view($viewName, $data);
		$this->load->view("common/footer");
	}

	private function requireLogin()
	{
		$segment = $this->uri->segment(1);
		if($segment=='cron'){
			return;
		}
		$acl = unserialize(ACL);
		$userlogin = $this->session->userdata('userlogin');
		if (in_array($segment, $acl) && !$userlogin) {
			$this->session->set_flashdata('referrer', uri_string());
			redirect(base_url(array('user', 'login')));
		}
	}

	protected function getUrlValFromId($arrSymbol, $key)
	{
		foreach ($arrSymbol as $item) {
			if ($item->getId() == $key) {
				return $item->getUrlVal();
			}
		}
		return "";
	}

	protected function getIdFromUrlVal($aVal, $val)
	{
		foreach ($aVal as $obj) {
			if ($obj->getUrlVal() == $val) {
				return $obj->getId();
			}
		}

		return - 1;
	}

	protected function getAgent()
	{
		$agent = '';
		try {
			$agent = $this->input->user_agent();
			if (!$agent) {
				$this->load->library('user_agent');
				if ($this->agent->is_browser()) {
					$agent = $this->agent->browser() . ' ' . $this->agent->version();
				} else {
					if ($this->agent->is_robot()) {
						$agent = $this->agent->robot();
					} else {
						if ($this->agent->is_mobile()) {
							$agent = $this->agent->mobile();
						} else {
							$agent = 'Unidentified User Agent';
						}
					}
				}
			} else {
				$agent = 'Unidentified User Agent';
			}
		} catch (Exception $e) {
			$agent = 'Unidentified User Agent';
		}
		return $agent;
	}

	protected function checkIfDownloadDataValid($data)
	{
		if (strpos($data, ERR_CSV_NORMAL) > 0 || strpos($data, ERR_CSV_CNX) > 0) {
			return false;
		}
		return true;
	}

	protected function encodeURL($sym)
	{
		if (strpos($sym, '&')) {
			$sym = str_replace('&', '%26', $sym);
		}
		if (strpos($sym, ' ')) {
			$sym = str_replace(' ', '%20', $sym);
		}
		return $sym;
	}
    
    protected function checkDate($date)
    {
        //format d-M-Y
        list($dd, $mm, $yyyy) = explode('-', $date);
        $arrMonth = array(
            1 => 'Jan',
            'Feb',
            'Mar',
            'Apr',
            'May',
            'Jun',
            'Jul',
            'Aug',
            'Sep',
            'Oct',
            'Nov',
            'Dec');
        foreach ($arrMonth as $k => $m) {
            if ($m == $mm) {
                $mm = $k;
            }
        }

        if (!is_numeric($yyyy) || !is_numeric($mm) || !is_numeric($dd)) {
            return false;
        }
        return checkdate($mm, $dd, $yyyy);
    }
}
?>